package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbDeviceDetailRepository extends JpaRepository<CmdbDeviceDetail, Integer>, JpaSpecificationExecutor<CmdbDeviceDetail> {
    List<CmdbDeviceDetail> findAllByDeviceDetailIdIn(List<Long> id);

    @Query(value = "select d from CmdbDeviceDetail d where d.isDelete = 0 " +
            "and (:#{#param.deviceDetailCode} is null or d.deviceDetailCode like concat('%', :#{#param.deviceDetailCode}, '%')) " +
            "and (:#{#param.deviceStandardCode} is null or d.deviceStandardCode = :#{#param.deviceStandardCode}) " +
            "and (:#{#param.name} is null or d.name like concat('%', :#{#param.name}, '%')) " +
            "and (:#{#param.assertNum} is null or d.assertNum = :#{#param.assertNum}) " +
            "and (:#{#param.brand} is null or d.brand = :#{#param.brand}) " +
            "and (:#{#param.model} is null or d.model like concat('%', :#{#param.model}, '%')) " +
            "and (:#{#param.sn} is null or d.sn like concat('%', :#{#param.sn}, '%')) " +
            "and (:#{#param.manufactDate} is null or d.manufactDate = :#{#param.manufactDate}) " +
            "and (:#{#param.expirateDate} is null or d.expirateDate = :#{#param.expirateDate}) " +
            "and (:#{#param.deviceType} is null or d.deviceType = :#{#param.deviceType}) " +
            "and (:#{#param.lifeStatus} is null or d.lifeStatus = :#{#param.lifeStatus}) " +
            "and (:#{#param.uAmount} is null or d.uAmount = :#{#param.uAmount}) " +
            "and (:#{#param.cpuPortAmount} is null or d.cpuPortAmount = :#{#param.cpuPortAmount}) " +
            "and (:#{#param.memoryPortAmount} is null or d.memoryPortAmount = :#{#param.memoryPortAmount}) " +
            "and (:#{#param.diskPortAmount} is null or d.diskPortAmount = :#{#param.diskPortAmount}) " +
            "and (:#{#param.pciPortAmount} is null or d.pciPortAmount = :#{#param.pciPortAmount}) " +
            "and (:#{#param.elePortAmount} is null or d.elePortAmount = :#{#param.elePortAmount}) " +
            "and (:#{#param.memoryTotalCapacity} is null or d.memoryTotalCapacity = :#{#param.memoryTotalCapacity}) " +
            "and (:#{#param.machineRoomId} is null or d.machineRoomId = :#{#param.machineRoomId}) " +
            "and (:#{#param.machineCabinetId} is null or d.machineCabinetId = :#{#param.machineCabinetId}) " +
            "and (:#{#param.machineCabinetUId} is null or d.machineCabinetUId = :#{#param.machineCabinetUId}) " +
            "and (:#{#param.inputPriceWithTax} is null or d.inputPriceWithTax = :#{#param.inputPriceWithTax}) " +
            "and (:#{#param.inputPriceNoTax} is null or d.inputPriceNoTax = :#{#param.inputPriceNoTax}) " +
            "and (:#{#param.user} is null or d.user = :#{#param.user}) " +
            "and (:#{#param.purchaseDate} is null or d.purchaseDate = :#{#param.purchaseDate}) ")
    Page<CmdbDeviceDetail> searchByParam(Pageable pageable, CmdbDeviceDetail param);


    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_device_detail set is_delete = :deleted where device_detail_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    List<CmdbDeviceDetail> findAllByIsDelete(Integer noDelete);


    List<CmdbDeviceDetail> findAllBySnIn(List<String> sn);

    CmdbDeviceDetail findOneByDeviceDetailCode(String deviceDetailCode);

    List<CmdbDeviceDetail> findAllBySnAndIsDelete(String sn, Integer noDelete);

    List<CmdbDeviceDetail> findAllByDeviceDetailCodeIn(List<String> deviceDetailCodeList);
}

